<?php
/**
 *  * 系统-受国家计算机软件著作权保护 - !
 * =========================================================
 * Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.ohyu.cn
 * 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * User: ohyueo
 * Date: 2021/5/17
 * Time: 14:17
 */

namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use app\admin\models\EscortUserAddress;
use app\admin\models\EscortUserCollect;
use app\admin\traits\AdminAuth;
use app\admin\models\EscortUserList;
use app\admin\models\EscortUserLogin;
use think\facade\Session;
use app\handler\MoneyLogHandler;
use app\admin\models\EscortUserInfo;

class User extends Common
{
    use AdminAuth;
    public function sousuo(){
        return View::fetch();
    }
    public function login(){
        Session::delete('Auto_admin_name','think');
        Session::delete('Auto_admin_token','think');

        Session::clear();
        return redirect('/admin/Login/index');exit;
        echo '<script>parent.location.reload();</script>';exit;
    }
    public function zjmoney(){
        $st=input("param.st");
        $id=input("param.user");
        if(!$id){ $data['msg']='用户不能为空';return json($data);exit;}
        $money=input("param.money");
        $sm=input("param.sm");
        if(!$sm){ $data['msg']='操作说明不能为空';return json($data);exit;}
        //根据模型里面的baseModel方法获取数据
        $user = EscortUserList::findData(['where'=>['id'=>$id]]);
        //$user = EscortUserList::find($id);
        if($st==1){//增加
            //var_dump($id);exit;
            // 加钱
            $user->money += $money;
            $user->price += $money;
            $user->save();
            $text = '增加了用户id='.$id.'余额'.$money;
            $this->writeActionLog($text);
            // 资金记录
            MoneyLogHandler::add($user,$money,$st,2,$sm);
        }else if($st==2){//减少
            // 加钱
            $user->money -= $money;
            $user->save();
            $text = '减少了用户id='.$id.'余额'.$money;
            $this->writeActionLog($text);
            // 资金记录
            MoneyLogHandler::add($user,$money,$st,2,$sm);
        }else if($st==3){//增加
            // 积分
            $user->integral += $money;
            $user->save();
            $text = '增加了用户id='.$id.'积分'.$money;
            $this->writeActionLog($text);
            // 资金记录
            MoneyLogHandler::addint($user,$money,1,2,$sm);
        }else if($st==4){//减少
            // 积分
            $user->integral -= $money;
            $user->save();
            $text = '减少了用户id='.$id.'积分'.$money;
            $this->writeActionLog($text);
            // 资金记录
            MoneyLogHandler::addint($user,$money,2,2,$sm);
        }
        $data = array('status' => 1,'msg' => '修改成功');
        return json($data);exit;
    }
    public function userlist(){
        $permis = $this->getPermissions('User/userlist');
        View::assign('data', $this->actions);
        View::assign('permis', $permis);
        if(request()->isPost()){
            $page=input("param.page");
            if(!$page){
                $page=1;
            }
            $limit=input("param.limit");
            if(!$limit){
                $limit=10;//每页显示条数
            }
            $where=[];
            $name=input("param.name");
            if($name){
                $where[]=['nick','like','%'.$name.'%'];
            }
            $id=input("param.id");
            if($id){
                $where[]=['id','=',$id];
            }
            $status=input("param.status");
            if($status){
                $where[]=['status','=',$status];
            }
            //$pre = ($page-1)*$limit;//起始页数
            //根据模型里面的baseModel方法获取数据
            $count = EscortUserList::countData(['where'=>$where]);
            $data=EscortUserList::findAllData(['where'=>$where,'order'=>['id'=>'desc'],'page'=>$page,'limit'=>$limit,'field'=>'*']);
            if($data){
                for($i=0;$i<count($data);$i++){
                    //使用Auto_user_login查询
                    $res=EscortUserLogin::findData(['where'=>['uid'=>$data[$i]['id']]]);
                    if($res){
                        $data[$i]['reg_time']=$res['reg_time'];
                        $data[$i]['rece_login_time']=$res['rece_login_time'];
                        $data[$i]['mobile']=$res['mobile'];
                        $data[$i]['email']=$res['email'];
                    }else{
                        $data[$i]['reg_time']='';
                        $data[$i]['rece_login_time']='';
                        $data[$i]['mobile']='';
                        $data[$i]['email']='';
                    }

                    $img=$data[$i]['headimg'];
                    if(!$img){
                        $data[$i]['headimgx']=getFullImageUrl2('');//getFullImageUrl2('')是一个方法，返回一个空的图片地址
                    }else{
                        $data[$i]['headimgx']=getFullImageUrl2($data[$i]['headimg']);
                    }
                }
            }
            //查询产品
            // $count = Db::name('Auto_user_list')->alias('u')->where($where)->count();
            // $data=Db::name('Auto_user_list')->alias('u')
            //     ->join('Auto_user_login l', 'u.id = l.uid', 'LEFT')
            //     ->join('Auto_user_info i', 'u.id = i.user_id', 'LEFT')
            //     ->where($where)
            //     ->limit($pre,$limit)
            //     ->field('u.id,u.nick,u.headimg,u.status,u.money,u.integral,l.reg_time,l.rece_login_time,l.mobile,l.email')
            //     ->order('id desc')
            //     ->select()->all();
            // if($data){
            //     for($i=0;$i<count($data);$i++){
            //         $img=$data[$i]['headimg'];
            //         if(!$img){
            //             $data[$i]['headimg']=getFullImageUrl2('');
            //         }else{
            //             $data[$i]['headimg']=getFullImageUrl2($data[$i]['headimg']);
            //         }
            //     }
            // }
            $res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
            return json($res);
        }
        return View::fetch();
    }
    public function userlist_hei(){
        $permis = $this->getPermissions('User/userlist_hei');
        View::assign('data', $this->actions);
        View::assign('permis', $permis);
        return View::fetch();
    }
    //加入黑名单解除黑名单
    public function setStatus(){
        $st=input("param.st");
        $id=input("param.id");
        if(!$id){ $data['msg']='用户不能为空';return json($data);exit;}
        if($st==1){
            //根据模型里面的baseModel方法查询单条数据
            $user = EscortUserList::findData(['where'=>['id'=>$id]]);
            //$user = EscortUserList::find($id);
            $user->status= $st;
            $user->save();
            $msg='解除成功';
        }else if($st==2){
            //根据模型里面的baseModel方法查询单条数据
            $user = EscortUserList::findData(['where'=>['id'=>$id]]);
            //$user = UserList::find($id);
            $user->status= $st;
            $user->save();
            $msg='加入成功';
        }

        $data = array('status' => 1,'msg' => $msg);
        return json($data);exit;
    }
    //修改用户昵称
    public function setuserInfo(){
        $st=input("param.st");
        $id=input("param.id");
        if(!$id){ $data['msg']='用户不能为空';return json($data);exit;}

        if($st==1){//nick
            //根据模型里面的baseModel方法查询单条数据
            $user = EscortUserList::findData(['where'=>['id'=>$id]]);
            //$user = EscortUserList::find($id);
            $nick=input("param.value");
            if(!$nick){ $data['msg']='用户昵称不能为空';return json($data);exit;}
            $user->nick= $nick;
            $user->save();
            $text = '修改了用户id='.$id.'的昵称'.$nick;
            $this->writeActionLog($text);
        }else if($st==2){//mobile
            //根据模型里面的baseModel方法查询单条数据
            $user = EscortUserLogin::findData(['where'=>['uid'=>$id]]);
            //$user = EscortUserLogin::where('uid',$id)->find();
            $mobile=input("param.value");
            if(!$mobile){ $data['msg']='手机号不能为空';return json($data);exit;}
            $user->mobile= $mobile;
            $user->save();
            $text = '修改了用户id='.$id.'的手机号'.$mobile;
            $this->writeActionLog($text);
        }else if($st==3){//email
            //根据模型里面的baseModel方法查询单条数据
            $user = EscortUserLogin::findData(['where'=>['uid'=>$id]]);
            //$user = EscortUserLogin::where('uid',$id)->find();
            $email=input("param.value");
            if(!$email){ $data['msg']='邮箱不能为空';return json($data);exit;}
            $user->email= $email;
            $user->save();
            $text = '修改了用户id='.$id.'的邮箱'.$email;
            $this->writeActionLog($text);
        }
        $data = array('status' => 1,'msg' => '修改成功');
        return json($data);exit;
    }
    public function userinfo(){
        $id=input("param.id");
        //根据模型里面的baseModel方法查询单条数据
        $user = EscortUserList::findData(['where'=>['id'=>$id]]);
        //根据模型里面的baseModel方法查询单条数据
        $res = EscortUserLogin::findData(['where'=>['uid'=>$id]]);
        $user['reg_time']=$res['reg_time'];
        $user['rece_login_time']=$res['rece_login_time'];
        $user['mobile']=$res['mobile'];
        $user['email']=$res['email'];

        // $user=Db::name('Auto_user_list')->alias('u')
        //     ->join('Auto_user_login l', 'u.id = l.uid', 'LEFT')
        //     ->where('u.id',$id)
        //     ->field('u.id,u.nick,u.headimg,u.status,u.money,l.reg_time,l.rece_login_time,l.mobile,l.email')
        //     ->find();
        if($user){
            if(!$user['headimg']){
                $user['headimg']=getFullImageUrl('');//
            }else{
                $user['headimg']=getFullImageUrl($user['headimg']);
            }
            if(!$user['nick']){
                $user['nick']='无';
            }
            if(!$user['mobile']){
                $user['mobile']='无';
            }
            if(!$user['email']){
                $user['email']='无';
            }
        }
        View::assign('user', $user);
        return View::fetch();
    }
    public function add(){
        if(request()->isPost()){
            $data = array('status' => 0,'msg' => '未知错误');
            $nick=input("param.nick");
            //if(!$nick){ $data['msg']='用户昵称不能为空';return json($data);exit;}
            $pwd=input("param.password");
            //if(!$pwd){ $data['msg']='用户密码不能为空';return json($data);exit;}
            $img=input("param.headimg");
            //if(!$img){ $data['msg']='用户头像不能为空';return json($data);exit;}

            $status=input("param.status");

            $arr = array(
                'nick' => $nick,
                'headimg' => $img,
                'status' => $status
            );
            //根据模型里面的baseModel方法添加数据
            $res = EscortUserList::addData($arr);
            $addloan=$res->id;
            //$addloan=Db::name('Auto_user_list')->insertGetId($arr);
            if($addloan){

                //根据模型里面的baseModel方法添加数据
                EscortUserInfo::addData(['user_id'=>$addloan,'timo'=>0,'name'=>'','mobile'=>'']);
                //添加联系人
                //Db::name('Auto_user_info')->insert(['user_id'=>$addloan]);

                $data['status']=1;
                $t=date('Y-m-d H:i:s');
                //登录表
                $logarr=array(
                    'uid' => $addloan,
                    'reg_time' => $t
                );
                if($pwd){
                    $logarr['password'] = password_hash($pwd, PASSWORD_DEFAULT);
                }
                //根据模型里面的baseModel方法添加数据
                EscortUserLogin::addData($logarr);
                //Db::name('Auto_user_login')->insert($logarr);

                $text = '添加了用户id='.$addloan;
                $this->writeActionLog($text);
            }else{
                $data['msg']='添加失败';
            }
            return json($data);exit;
        }
        return View::fetch('edit');
    }
    public function edit(){
        if(request()->isPost()){
            $data = array('status' => 0,'msg' => '未知错误');
            $id=input("param.id");
            if(!$id){ $data['msg']='修改用户不能为空';return json($data);exit;}
            $nick=input("param.nick");
            if(!$nick){ $data['msg']='用户昵称不能为空';return json($data);exit;}
            $pwd=input("param.password");
            $img=input("param.headimg");
            if(!$img){ $data['msg']='用户头像不能为空';return json($data);exit;}
            $status=input("param.status");

            //根据模型里面的baseModel方法查询条数量
            $u = EscortUserList::countData(['where'=>['id'=>$id]]);
            //查询用户是否存在
            //$u=Db::name('Auto_user_list')->where('id',$id)->count();
            if(!$u){
                $data['msg']='用户不存在';return json($data);exit;
            }
            //根据模型里面的baseModel方法查询单条数据
            $res = EscortUserLogin::findData(['where'=>['uid'=>$id]]);
            //$res=Db::name('Auto_user_info')->where('user_id',$id)->find();
            if(!$res){
                //添加联系人
                //Db::name('Auto_user_info')->insert(['user_id'=>$id]);
                //根据模型里面的baseModel方法添加数据
                EscortUserInfo::addData(['user_id'=>$id]);
            }

            $xg=0;
            if($pwd){
                //登录表
                $logarr=array(
                    'password' => password_hash($pwd, PASSWORD_DEFAULT)
                );
                //根据模型里面的baseModel方法修改数据
                $xg=EscortUserLogin::updatesData(['where'=>['uid'=>$id],'data'=>$logarr]);
                //Db::name('user_login')->where('uid',$id)->update($logarr);
                $text = '修改了用户id='.$id.'的密码';
                $this->writeActionLog($text);
            }

            //修改资料
            $xrr=array(
                'nick'=>$nick,
                'headimg' => $img,
                'status' => $status
            );
            //根据模型里面的baseModel方法修改数据
            $addloan=EscortUserList::updatesData(['where'=>['id'=>$id],'data'=>$xrr]);
            //$addloan=Db::name('user_list')->where('id',$id)->update($xrr);
            if($addloan || $xg){
                $data['status']=1;

                $text = '修改了用户资料id='.$id;
                $this->writeActionLog($text);
            }
            $data['status']=1;
            return json($data);exit;
        }
        return View::fetch('edit');
    }
    public function deluser(){
        $data = array('status' => 0,'msg' => '未知错误');
        $array=input("param.id");
        if(!$array){
            $data['msg']='参数错误';return json($data);exit;
        }
        $arr=explode(",",$array);
        for($i=0;$i<count($arr);$i++){
            //删除数据
            if($arr[$i]){
                //根据模型里面的baseModel方法删除数据
                EscortUserList::deleteData(['where'=>['id'=>$arr[$i]]]);
                //Db::name('Auto_user_list')->where('id',$arr[$i])->delete();
                $text = '删除了用户id='.$arr[$i].'的数据user_list';
                $this->writeActionLog($text);
                //根据模型里面的baseModel方法删除数据
                EscortUserLogin::deleteData(['where'=>['uid'=>$arr[$i]]]);
                //Db::name('Auto_user_login')->where('uid',$arr[$i])->delete();
                $text = '删除了用户登录数据id='.$arr[$i].'的数据user_login';
                $this->writeActionLog($text);
            }
        }
        $data['status']=1;
        return json($data);
    }

	public function collect(){
 		$dao=input('param.dao');
		if(request()->isPost() || $dao==1){
			$page=input('param.page');
			if(!$page){
				$page=1;
			}
			$limit=input('param.limit');
			if(!$limit){
				$limit=10;//每页显示条数
			}
			$where=[];
			$id=input('param.id');
			if($id){
				$where[]=['id','=',$id];
			}
			$uid=input('param.uid');
			if($uid){
				$where[]=['uid','like','%'.$uid.'%'];
			}
			$shopid=input('param.shopid');
			if($shopid){
				$where[]=['shopid','like','%'.$shopid.'%'];
			}
			$addtime=input('param.addtime');
			if($addtime){
				$where[]=['addtime','like','%'.$addtime.'%'];
			}
            $count = EscortUserCollect::countData($where);
			if($dao!=1){
				$data=EscortUserCollect::findAllData($where, ['id'=>'desc'], $page, $limit,$field='*');
			}
			if($dao==1){
				$data=EscortUserCollect::findAllData($where,['id'=>'desc']);
			}
			$res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
			return json($res);
		}
		$permis = $this->getPermissions('User/collect');
		View::assign('data', $this->actions);
		View::assign('permis', $permis);
		return View::fetch();
	}
	public function addcollect(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$uid=input('param.uid');
			$shopid=input('param.shopid');
			$addtime=input('param.addtime');
			$arr=array(
				'uid'=>$uid,
				'shopid'=>$shopid,
				'addtime'=>$addtime,
			);
			$res=EscortUserCollect::addData($arr);
            $id=$res->id;
			$text = '添加了商品收藏-id='.$id;
			$this->writeActionLog($text);
			$data['status'] = 1;
			return json($data);exit;
		}
		return View::fetch('editcollect');
	}
	public function editcollect(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$uid=input('param.uid');
			$shopid=input('param.shopid');
			$addtime=input('param.addtime');
			$arr=array(
				'uid'=>$uid,
				'shopid'=>$shopid,
				'addtime'=>$addtime,
			);
			$id = input('param.id');
			if ($id){
				EscortUserCollect::updatesData($id,$arr);
				$text = '修改了商品收藏-id='.$id;
				$this->writeActionLog($text);
				$data['status'] = 1;
				return json($data);exit;
			}
		}
		return View::fetch('editcollect');
	}
	public function delcollect(){
 		$data = array('status' => 0,'msg' => '未知错误');
		$array=input("param.id");
		if(!$array){
			$data['msg']='参数错误';return json($data);exit;
		}
		$arr=explode(",",$array);
		for($i=0;$i<count($arr);$i++){
			if($arr[$i]){
				EscortUserCollect::deleteData($arr[$i]);
				$text = '删除了商品收藏id='.$arr[$i].'的数据';
				$this->writeActionLog($text);
			}
		}
		$data['status']=1;
		return json($data);
	}
	public function address(){
 		$dao=input('param.dao');
		if(request()->isPost() || $dao==1){
			$page=input('param.page');
			if(!$page){
				$page=1;
			}
			$limit=input('param.limit');
			if(!$limit){
				$limit=10;//每页显示条数
			}
			$where=[];
			$id=input('param.id');
			if($id){
				$where[]=['id','=',$id];
			}
			$name=input('param.name');
			if($name){
				$where[]=['name','like','%'.$name.'%'];
			}
			$mobile=input('param.mobile');
			if($mobile){
				$where[]=['mobile','like','%'.$mobile.'%'];
			}
			$uid=input('param.uid');
			if($uid){
				$where[]=['uid','like','%'.$uid.'%'];
			}
			$province=input('param.province');
			if($province){
				$where[]=['province','like','%'.$province.'%'];
			}
			$city=input('param.city');
			if($city){
				$where[]=['city','like','%'.$city.'%'];
			}
			$area=input('param.area');
			if($area){
				$where[]=['area','like','%'.$area.'%'];
			}
			$address=input('param.address');
			if($address){
				$where[]=['address','like','%'.$address.'%'];
			}
			$default=input('param.default');
			if($default){
				$where[]=['default','like','%'.$default.'%'];
			}
			$addtime=input('param.addtime');
			if($addtime){
				$where[]=['addtime','like','%'.$addtime.'%'];
			}
            $dataarr=['where'=>$where];
			$count = EscortUserAddress::countData($dataarr);
			if($dao!=1){
                $dataarr=['where'=>$where,'order'=>['id'=>'desc'],'page'=>$page,'limit'=>$limit,'field'=>'*'];
				$data=EscortUserAddress::findAllData($dataarr);
			}
			if($dao==1){
                $dataarr=['where'=>$where,'order'=>['id'=>'desc']];
				$data=EscortUserAddress::findAllData($dataarr);
			}
			$res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
			return json($res);
		}
		$permis = $this->getPermissions('User/address');
		View::assign('data', $this->actions);
		View::assign('permis', $permis);
		return View::fetch();
	}
	public function addaddress(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$name=input('param.name');
			$mobile=input('param.mobile');
			$uid=input('param.uid');
			$province=input('param.province');
			$city=input('param.city');
			$area=input('param.area');
			$address=input('param.address');
			$default=input('param.default');
			$addtime=input('param.addtime');
			$arr=array(
				'name'=>$name,
				'mobile'=>$mobile,
				'uid'=>$uid,
				'province'=>$province,
				'city'=>$city,
				'area'=>$area,
				'address'=>$address,
				'default'=>$default,
				'addtime'=>$addtime,
			);
			$res=EscortUserAddress::addData($arr);
            $id=$res->id;
			$text = '添加了联系人列表-id='.$id;
			$this->writeActionLog($text);
			$data['status'] = 1;
			return json($data);exit;
		}
		return View::fetch('editaddress');
	}
	public function editaddress(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$name=input('param.name');
			$mobile=input('param.mobile');
			$uid=input('param.uid');
			$province=input('param.province');
			$city=input('param.city');
			$area=input('param.area');
			$address=input('param.address');
			$default=input('param.default');
			$addtime=input('param.addtime');
			$arr=array(
				'name'=>$name,
				'mobile'=>$mobile,
				'uid'=>$uid,
				'province'=>$province,
				'city'=>$city,
				'area'=>$area,
				'address'=>$address,
				'default'=>$default,
				'addtime'=>$addtime,
			);
			$id = input('param.id');
			if ($id){
				EscortUserAddress::updatesData($id,$arr);
				$text = '修改了联系人列表-id='.$id;
				$this->writeActionLog($text);
				$data['status'] = 1;
				return json($data);exit;
			}
		}
		return View::fetch('editaddress');
	}
	public function deladdress(){
 		$data = array('status' => 0,'msg' => '未知错误');
		$array=input("param.id");
		if(!$array){
			$data['msg']='参数错误';return json($data);exit;
		}
		$arr=explode(",",$array);
		for($i=0;$i<count($arr);$i++){
			if($arr[$i]){
				EscortUserAddress::deleteData($arr[$i]);
				$text = '删除了联系人列表id='.$arr[$i].'的数据';
				$this->writeActionLog($text);
			}
		}
		$data['status']=1;
		return json($data);
	}
}